package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.BusinessServiceModel;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Raven on 2017/12/13.
 */

public class BusinessAdapter extends RecyclerArrayAdapter<BusinessServiceModel.ServiceBean> {

    private Context context;

    public BusinessAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new BusinessViewHolder(parent);
    }

    class BusinessViewHolder extends BaseViewHolder<BusinessServiceModel.ServiceBean> {

        private TextView tvBusinessServiceName;
        private ImageView ivBusinessService;
        private TextView itemTvAbstract;
        private TextView itemTvPrice;
        private TextView itemTvFrontMoney;
        private TextView itemTvMemPrice;

        public BusinessViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_business_layout);
            tvBusinessServiceName = $(R.id.tv_business_service_name);
            ivBusinessService = $(R.id.iv_business_service);
            itemTvAbstract = $(R.id.item_tv_abstract);
            itemTvPrice = $(R.id.item_tv_price);
            itemTvFrontMoney = $(R.id.item_tv_front_money);
            itemTvMemPrice = $(R.id.item_tv_mem_price);
        }

        @Override
        public void setData(BusinessServiceModel.ServiceBean data) {
            super.setData(data);
            tvBusinessServiceName.setText(data.getServiceName());
            GlideUtils.loadImage(context,data.getLogo(), ivBusinessService, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            },R.mipmap.default_banner,R.mipmap.default_banner);
            String descriptionHtml = data.getDescription();
            CharSequence charSequence = "";
            if(descriptionHtml != null) {
                charSequence = Html2Text(descriptionHtml);
            }

            itemTvAbstract.setText(TextUtils.isEmpty(charSequence)?"":charSequence);
            if(data.getStoreMemberPrice() > 0 ) {
                itemTvMemPrice.setVisibility(View.VISIBLE);
                itemTvMemPrice.setText("￥" + String.format("%.2f", data.getStoreMemberPrice()));
            }else{
                itemTvMemPrice.setVisibility(View.GONE);
            }
            itemTvPrice.setText(""+String.format("%.2f",data.getServicePrice()));
            if(data.getReservationPrice() > 0) {
                itemTvFrontMoney.setVisibility(View.VISIBLE);
                itemTvFrontMoney.setText("￥" + String.format("%.2f", data.getReservationPrice()));
            }else{
                itemTvFrontMoney.setVisibility(View.GONE);
            }
        }
    }

    //从html中提取纯文本
    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签
            textStr = htmlStr;
        } catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); }
        //剔除空格行
        textStr=textStr.replaceAll("[ ]+", " ");
        textStr=textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
        return textStr;// 返回文本字符串
    }
}
